package com.lahiyam.study.mybatis.config;

import java.util.ResourceBundle;

import javax.sql.DataSource;

import org.h2.jdbcx.JdbcDataSource;

import com.mysql.cj.jdbc.MysqlDataSource;

/**
 * DataSource
 * 
 * @author 00fly
 * @version [版本号, 2021年4月5日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class MyDataSourceFactory
{
    private static DataSource dataSource;
    
    // 静态初始化 DataSource
    static
    {
        ResourceBundle config = ResourceBundle.getBundle("jdbc");
        String driver = config.getString("jdbc.driver");
        switch (driver)
        {
            case "org.h2.Driver":
                dataSource = new JdbcDataSource();
                ((JdbcDataSource)dataSource).setUrl(config.getString("jdbc.url"));
                ((JdbcDataSource)dataSource).setUser(config.getString("jdbc.username"));
                ((JdbcDataSource)dataSource).setPassword(config.getString("jdbc.password"));
                break;
            
            case "com.mysql.cj.jdbc.Driver":
                dataSource = new MysqlDataSource();
                ((MysqlDataSource)dataSource).setUrl(config.getString("jdbc.url"));
                ((MysqlDataSource)dataSource).setUser(config.getString("jdbc.username"));
                ((MysqlDataSource)dataSource).setPassword(config.getString("jdbc.password"));
                break;
            
            default:
                break;
        }
    }
    
    public static DataSource getDataSource()
    {
        return dataSource;
    }
}
